//
//  YTPhoneChangeViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/22.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTPhoneChangeViewController.h"
#import "YTChangePhoneViewModel.h"
#import "YTCountryCodeViewController.h"

@interface YTPhoneChangeViewController ()<YTCountryCodeDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumText;
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeText;
@property (weak, nonatomic) IBOutlet UIButton *sendVerifyBtn;
@property (weak, nonatomic) IBOutlet UIButton *completeBtn;
@property (weak, nonatomic) IBOutlet UIButton *selectCountryCode;
@property (strong, nonatomic) YTChangePhoneViewModel *viewModel;
@end

@implementation YTPhoneChangeViewController

- (void)initialize {
    _viewModel = [[YTChangePhoneViewModel alloc] init];
    _viewModel.oldVerifyCode = self.oldVerifyCode;
}

- (void)bindViewModel {
    RAC(_viewModel, phoneNum) = _phoneNumText.rac_textSignal;
    RAC(_viewModel, verifyCode) = _verifyCodeText.rac_textSignal;
    RAC(_viewModel, areaCode) = RACObserve(_selectCountryCode, currentTitle);
    _sendVerifyBtn.rac_command = _viewModel.sendVerifyCommand;
    _completeBtn.rac_command = _viewModel.completeCommand;
    [self responseUserInteracte];
}

/** UI逻辑*/
- (void)responseUserInteracte {
    UIColor *(^validateColor)(NSNumber *) = ^(NSNumber *enabled) {
        return enabled.boolValue ? [UIColor blackColor] : [UIColor redColor];
    };
    RAC(_phoneNumText, textColor) = [[_viewModel.phoneValidate distinctUntilChanged] map:validateColor];
    RAC(_verifyCodeText, textColor) = [[_viewModel.verifyValidate distinctUntilChanged] map:validateColor];
    
    RAC(_sendVerifyBtn, backgroundColor) = [[_viewModel.phoneValidate distinctUntilChanged] map:^id(NSNumber *value) {
        return value.boolValue ? kMainBlueColor : [UIColor lightGrayColor];
    }];
    
    @weakify(self);
    [[_viewModel.sendVerifyCommand.executionSignals switchToLatest]
     subscribeNext:^(NSString *title) {
         @strongify(self);
         [self.sendVerifyBtn setTitle:title forState:UIControlStateNormal];
     }];
    
    RAC(_completeBtn, backgroundColor) = [_viewModel.completeCommand.enabled map:^id(NSNumber *enabled) {
        if (enabled.boolValue) {
            [self.view endEditing:YES];
        }
        return enabled.boolValue ? kMainBlueColor : [UIColor lightGrayColor];
    }];
    
    [[_viewModel.completeCommand.executionSignals switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        [self performSegueWithIdentifier:@"YTChangePhoneSuccess" sender:nil];
    }];
}

- (IBAction)selectCountryCodeAction:(UIButton *)sender {
    YTCountryCodeViewController *code = [[YTCountryCodeViewController alloc] init];
    code.delegate = self;
    [self.navigationController pushViewController:code animated:YES];
}

- (void)didSelectWithCountryCode:(NSString *)countryCode {
    [self.selectCountryCode setTitle:[NSString stringWithFormat:@"+ %@", countryCode] forState:UIControlStateNormal];
}

@end
